package com.gd.coreservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gd.coreservice.entity.Admin;
import com.gd.coreservice.entity.vo.AdminVo;
import com.gd.coreservice.mapper.AdminMapper;
import com.gd.coreservice.service.AdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author testjava
 * @since 2024-03-23
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {
@Autowired
private AdminMapper adminMapper;


    @Override
    public AdminVo login(String username, String password) {
        try{
            Admin login = adminMapper.login(username, password);
            AdminVo adminVo = new AdminVo();
            BeanUtils.copyProperties(login,adminVo);
            return adminVo;
        }
        catch (Exception e)
        {
            return null;
        }
    }

    @Override
    public String findId(String username) {
        return adminMapper.findIdByusername(username);
    }

    @Override
    public boolean register(String username, String password) {
        boolean istrue = false;
        Admin admin = new Admin();
        admin.setUsername(username);
        admin.setPassword(password);
        try{
            if (0!=baseMapper.insert(admin)){
                istrue = true;
            }
            return istrue;
        }
        catch (Exception e){
            return istrue;
        }

    }

    @Override
    public List<Admin> findAll() {
        QueryWrapper<Admin> objectQueryWrapper = new QueryWrapper<>();
        return baseMapper.selectList(objectQueryWrapper);
    }

    @Override
    public Admin findOne(String iid) {
        return baseMapper.selectById(iid);
    }

    @Override
    public boolean updatePassword(String iid, String password, String newpassword) {
        try {
            Admin admin = new Admin();
            admin.setAid(iid);
            admin.setPassword(newpassword);
            UpdateWrapper<Admin> UpdateWrapper = new UpdateWrapper<>();
            UpdateWrapper.eq("Aid",iid);
            UpdateWrapper.eq("password",password);
            int result = baseMapper.update(admin,UpdateWrapper);
            if (0!=result){
                return true;
            }
            else return false;
        }catch (Exception e){
                return false;
        }

    }

    @Override
    public boolean insertAdmin(Admin admin) {
        boolean istrue = false;
        try{
            if (0!=baseMapper.insert(admin)){
                istrue = true;
            }
            return istrue;
        }
        catch (Exception e){
            return istrue;
        }
    }

    @Override
    public boolean updateAdmin(Admin admin) {
        boolean istrue = false;
        try{
            UpdateWrapper<Admin> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("Aid",admin.getAid());
            if (0!=baseMapper.update(admin,updateWrapper)){
                istrue = true;
            }
            return istrue;
        }
        catch (Exception e){
            return istrue;
        }
    }

    @Override
    public Integer deleteAdmin(String aid) {
        Integer i = baseMapper.deleteById(aid);
        return i ;
    }

    @Override
    public Integer deleteAdminBatch(List<String> aids) {
        return baseMapper.deleteBatchIds(aids);
    }

    @Override
    public Map<String, Object> getAdminPageData(Long pagenum, Long pagesize, String Ausername) {
        HashMap<String, Object> objectObjectHashMap = new HashMap<>();
        Page<Admin> page = new Page<>(pagenum,pagesize);
        QueryWrapper<Admin> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.like("username",Ausername);
        IPage<Admin> adminIPage = baseMapper.selectPage(page, objectQueryWrapper);

        objectObjectHashMap.put("data",adminIPage.getRecords());
        objectObjectHashMap.put("total",adminIPage.getTotal());
        return objectObjectHashMap;

    }

    @Override
    public int getAllTotal() {
        return baseMapper.selectCount(null);
    }


}
